package com.diko.product.service.impl;

import com.diko.product.entity.AttrEntity;
import com.diko.product.service.AttrService;
import com.diko.product.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diko.common.utils.PageUtils;
import com.diko.common.utils.Query;

import com.diko.product.dao.AttrGroupDao;
import com.diko.product.entity.AttrGroupEntity;
import com.diko.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key=(String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
        //使用正则表达式
        wrapper.and((obj)->{
            //key可能是属性分组的id或是模糊查询的分组名字
            obj.eq("attr_group_id",key).or().like("attr_group_name",key);
        });
    }
        if(catelogId==0){//分类id为0，表示查询所有的分组信息
        //这个方法可参照上面查找所有list的方法
            IPage<AttrGroupEntity> page=this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }else{
            wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page=this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);
        }
}

    /**
     * 根据分类id查出所有的分组以及这些组里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1.查询分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //2.查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrsVo attrGroupVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrGroupVo);

            //根据属性分组id查出所有属性
            List<AttrEntity> attrs = attrService.getAttrRelation(attrGroupVo.getAttrGroupId());
            attrGroupVo.setAttrs(attrs);
            return attrGroupVo;
        }).collect(Collectors.toList());

        return collect;
    }

}